<template>

  <el-dialog width="30%" :title="id == null ? '添加' : '编辑'" :visible.sync="dialogVisible" :close-on-click-modal="false">

    <el-form ref="main-form" status-icon :model="form" :rules="rules" label-width="120px" label-position="left" :inline="false" style="width: 400px;">
      
      <el-form-item label="标签" size="mini" prop="tagName">
        <el-input placeholder="" v-model="form.tagName" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>

    </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="close" size="mini">取 消</el-button>
        <el-button @click="submit" size="mini" type="primary" :loading="loading">提 交</el-button>
      </div>
  </el-dialog>
</template>

<script>

import ApiMain from '@/api/platform.mchTag';

export default {
  mixins: [
  ],
  props: [
    'statusOptions',
  ],
  data () {
    return {

      dialogVisible   : false,
      popDeleteVisible: false,
      loading         : false,

      mainQueryRaw: null,
      mainQuery: {
        countryNameSimple: '',
        countryNameEN: '',
        countryNameCN: '',
        ENorder: '',
        CNorder: '',
        status: 0,
      },

      form: {
        tagName: ''
      },

      rules: {
        tagName: [
          { required: true, message: '请输入标签', trigger: 'blur' },
        ],
      },

      id  : null,
      data: null,

    }
  },
  mounted () {
  },

  methods: {

    show (id) {

      this.reset();

      if (id) {
        this.id = id;
        this.fetchData();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        tagName: ''
      };
        
      this.id   = null;
      this.data = null;
    },

    async fetchData () {

      try {
        var re = await ApiMain.info({id: this.id});
        this.data = re;
        this.renderData();
      } catch (err) {
        this.data = null;
      }

    },

    renderData () {
      this.form.tagName = this.data.tagName;
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      if (this.id) {
        await this.submitSet();
      }
      else {
        await this.submitAdd();
      }

      this.loading = false;

    },

    async submitAdd () {

      try {
        await ApiMain.add(this.form);
        this.$message({message: '添加成功', type: 'success'});
        this.close();
      } catch (err) {
      }

    },

    async submitSet () {

      this.form.id = this.id;

      try {
        await ApiMain.set(this.form);
        this.$message({message: '修改成功', type: 'success'});
        this.close();
      } catch(e) {
      }

    },

    async submitDel () {
      this.popDeleteVisible = false;
      this.loading = true;

      try {
        await ApiMain.del({id: this.id})
        this.$message({message: '删除成功', type: 'success'});
        this.close();
      } catch(e) {
      }

      this.loading = false;

    },
  }
}
</script>

<style>

</style>
